package cn.qingyun.gis.modules.school.controller;

import cn.qingyun.gis.common.Result;
import cn.qingyun.gis.modules.init.system.annotation.AutoLog;
import cn.qingyun.gis.modules.init.system.base.controller.BasicController;
import cn.qingyun.gis.modules.init.system.query.QueryGenerator;
import cn.qingyun.gis.modules.school.entity.K12SchoolsProofread;
import cn.qingyun.gis.modules.school.entity.request.FuseCustomerChannelRequest;
import cn.qingyun.gis.modules.school.service.IK12SchoolsFuseCustomerChannelService;
import cn.qingyun.gis.modules.school.service.IK12SchoolsInfoService;
import cn.qingyun.gis.modules.school.service.IK12SchoolsProofreadService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;


/**
 * @Description: K12学校勘误表后台管理
 * @Version: V1.0
 */
@Api(tags = "K12学校勘误")
@RestController
@RequestMapping("/admin/school")
@Slf4j
public class K12SchoolsProofreadAdminController extends BasicController<K12SchoolsProofread, IK12SchoolsProofreadService> {
    @Resource
    private IK12SchoolsProofreadService k12SchoolsProofreadService;
    @Resource
    private IK12SchoolsInfoService k12SchoolsInfoService;
    @Resource
    private IK12SchoolsFuseCustomerChannelService ik12SchoolsFuseCustomerChannelService;


    @ApiOperation(value = "格式化K12学校勘误数据")
    @GetMapping("/clear")
    public Result<?> clearData() {
        k12SchoolsProofreadService.clearData();
        k12SchoolsInfoService.clearData();
        return Result.okData("格式化K12学校勘误数据, 请稍后");
    }

    /**
     * 初始化数据 全量未摸排K12学校数据
     *
     * @param file 全量未摸排K12学校数据
     * @return 返回信息
     */
    @ApiOperation(value = "初始化K12学校勘误数据csv文件")
    @PostMapping("/init")
    public Result<?> initData(@RequestParam("file") MultipartFile file) {
        k12SchoolsProofreadService.initData(file);
        return Result.okData("正在导入K12学校勘误数据.csv, 请稍后");
    }

    @PostMapping("/matchGrid")
    @ApiOperation("K12学校-匹配业务规划")
    public Result<?> matchGrid(@RequestParam("file") MultipartFile file) {
        k12SchoolsProofreadService.matchGrid(file);
        return Result.okData("正在进行K12学校的业务区域匹配");
    }


    /**
     * 分页列表查询
     *
     * @param k12SchoolsProofread
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "K12学校勘误表-分页列表查询")
    @ApiOperation(value = "K12学校勘误表-分页列表查询", notes = "K12学校勘误表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(K12SchoolsProofread k12SchoolsProofread,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<K12SchoolsProofread> queryWrapper = QueryGenerator.initQueryWrapper(k12SchoolsProofread, req.getParameterMap());
        Page<K12SchoolsProofread> page = new Page<>(pageNo, pageSize);
        IPage<K12SchoolsProofread> pageList = k12SchoolsProofreadService.page(page, queryWrapper);
        return Result.okData(pageList);
    }

    /**
     * 添加
     *
     * @param k12SchoolsProofread
     * @return
     */
    @AutoLog(value = "K12学校勘误表-添加")
    @ApiOperation(value = "K12学校勘误表-添加", notes = "K12学校勘误表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody K12SchoolsProofread k12SchoolsProofread) {
        k12SchoolsProofreadService.save(k12SchoolsProofread);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param k12SchoolsProofread
     * @return
     */
    @AutoLog(value = "K12学校勘误表-编辑")
    @ApiOperation(value = "K12学校勘误表-编辑", notes = "K12学校勘误表-编辑")
    @PostMapping(value = "/edit")
    public Result<?> edit(@RequestBody K12SchoolsProofread k12SchoolsProofread) {
        k12SchoolsProofreadService.updateById(k12SchoolsProofread);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "K12学校勘误表-通过id删除")
    @ApiOperation(value = "K12学校勘误表-通过id删除", notes = "K12学校勘误表-通过id删除")
    @GetMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id") String id) {
        k12SchoolsProofreadService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "K12学校勘误表-批量删除")
    @ApiOperation(value = "K12学校勘误表-批量删除", notes = "K12学校勘误表-批量删除")
    @GetMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids") String ids) {
        this.k12SchoolsProofreadService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "K12学校勘误表-通过id查询")
    @ApiOperation(value = "K12学校勘误表-通过id查询", notes = "K12学校勘误表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id") String id) {
        K12SchoolsProofread k12SchoolsProofread = k12SchoolsProofreadService.getById(id);
        if (k12SchoolsProofread == null) {
            return Result.error("未找到对应数据");
        }
        return Result.okData(k12SchoolsProofread);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param k12SchoolsProofread
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, K12SchoolsProofread k12SchoolsProofread) {
        return super.exportXls(request, k12SchoolsProofread, K12SchoolsProofread.class, "K12学校勘误表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, K12SchoolsProofread.class);
    }

    @GetMapping(value = "/getTableName")
    public Result<?> getLastFuseTableName() {
        return Result.okData(ik12SchoolsFuseCustomerChannelService.getLastFuseTableName());
    }
    @PostMapping(value = "/getData")
    public Result<?> getLastFuseTableName(@RequestBody FuseCustomerChannelRequest request) {
        return Result.okData(ik12SchoolsFuseCustomerChannelService.queryFuseCustData(request));
    }
}
